package main;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import utils.SmallTool;

import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022/3/12 13:20
 */
public class UpdateBlade4PmDoi {
    // 用于更新刀片4服务器 pm的doi 错误情况
    // 更新原则 有pmid的 更新进去 没有的不动
    public static String taiyiTable = "pubmed";
    //    public static String localTable = "pmid_doi_new";
    public static String localTable = "compare_doi2";

    @SneakyThrows
    public static void main(String[] args) {
//        DSFactory.setCurrentDSFactory(new DruidDSFactory());
//        DataSource ds = DSFactory.get("taiyiserver");
        Db useTaiyi = Db.use();

        String allDate = "select max(pmid) as pmid  from " + localTable;
        Entity query = useTaiyi.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("即将开始请提前关闭如果想的话");
        SmallTool.SleepMillis(10000L);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            String paramSql = "select pmid,doi_right from " + localTable + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = useTaiyi.query(paramSql);
            querys.forEach(entity -> {
                Integer pmid = entity.getInt("pmid");
                String doi = entity.getStr("doi_right");
                try {
                    updateData(pmid, doi, useTaiyi);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            });
        }


    }


    @SneakyThrows
    public static void updateData(Integer pmid, String doi, Db use) {
        Entity whereEn = new Entity();
        whereEn.setTableName(taiyiTable);
        whereEn.set("pmid", pmid);
        use.update(new Entity().set("doi", doi), whereEn);
    }

}
